﻿using Git.Storage.Provider.InStorage;
using Git.Storage.Provider.Move;
using Git.Storage.Provider.OutStorage;
using Git.Storage.Web.Lib;
using Git.Storage.Web.Models.Input.Agv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Git.Storage.Web.Controllers
{
    /// <summary>
    /// AGV
    /// </summary>
    public class AgvController : ApiController
    {
        /// <summary>
        /// 任务上报
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string TaskFeedback(TaskFeedbackModel model)
        {
            var a = model;
            try
            {
                string dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string msg = "任务反馈成功";
                string code = "200";
                string ordernum = model.TypeCode;
                int taskType = model.TaskType;

                //验证单号有没有


                // 处理异常情况
                var response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };
                // 根据业务逻辑进行任务反馈的处理
                if (string.IsNullOrEmpty(ordernum))
                {
                    code = "500";
                    msg = "任务单号不能为null";
                }
                if (model.Start == 1)
                {
                    // 处理异常情况
                    //return Json(response);
                    return "";
                }
                //入库
                if (taskType == 0)
                {
                    InStorageOrder agvFeedback = new InStorageOrder();
                    agvFeedback.AuditeInStorage(ordernum, taskType);
                }
                //出库
                else if (taskType == 1)
                {
                    OutStorageOrder agvFeedback = new OutStorageOrder();
                    agvFeedback.AuditeOutStorage(ordernum, taskType);
                }
                //移库
                else if (taskType == 2)
                {
                    MoveOrder agvFeedback = new MoveOrder();
                    agvFeedback.AuditeMoveStorage(ordernum, taskType);
                }
                else
                {
                    code = "500";
                    msg = "任务类型不对";
                }
                // 假设在此处对任务进行处理，并得到处理结果
                response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };

                return null;
            }
            catch (Exception ex)
            {
                // 处理异常情况
                var response = new
                {
                    code = 500,
                    msg = "任务反馈失败：" + ex.Message,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                return null;
            }
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="TypeCode">任务单号</param>
       /// <param name="TaskType">操作类型</param>
       /// <param name="Start">反馈结果 0成功，1失败</param>
       /// <returns></returns>
        [HttpGet]
        public string TaskFeedback1(string TypeCode,int TaskType,int Start)
        {
            try
            {
                string dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string msg = "任务反馈成功";
                string code = "200";
                string ordernum = TypeCode;
                int taskType = TaskType;

                //验证单号有没有


                // 处理异常情况
                var response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };
                // 根据业务逻辑进行任务反馈的处理
                if (string.IsNullOrEmpty(ordernum))
                {
                    code = "500";
                    msg = "任务单号不能为null";
                }
                if (Start == 1)
                {
                    // 处理异常情况
                    //return Json(response);
                    return "";
                }
                //入库
                if (taskType == 0)
                {
                    InStorageOrder agvFeedback = new InStorageOrder();
                    agvFeedback.AuditeInStorage(ordernum, taskType);
                }
                //出库
                else if (taskType == 1)
                {
                    OutStorageOrder agvFeedback = new OutStorageOrder();
                    agvFeedback.AuditeOutStorage(ordernum, taskType);
                }
                //移库
                else if (taskType == 2)
                {
                    MoveOrder agvFeedback = new MoveOrder();
                    agvFeedback.AuditeMoveStorage(ordernum, taskType);
                }
                else
                {
                    code = "500";
                    msg = "任务类型不对";
                }
                // 假设在此处对任务进行处理，并得到处理结果
                response = new
                {
                    code = code,
                    msg = msg,
                    dateTime = dateTime
                };

                return null;
            }
            catch (Exception ex)
            {
                // 处理异常情况
                var response = new
                {
                    code = 500,
                    msg = "任务反馈失败：" + ex.Message,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                return null;
            }
        }
        public string GetTest()
        {
            return "测试成功";
        }
    }
}